package com.sm.ali.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.ijpay.alipay.AliPayApi;
import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;
import com.sm.ali.entity.AliPayBean;
import com.sm.wx.vo.AjaxResult;
import com.sm.wx.vo.WxOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.RoundingMode;

/**
 * <p>IJPay 让支付触手可及，封装了微信支付、支付宝支付、银联支付常用的支付方式以及各种常用的接口。</p>
 *
 * <p>不依赖任何第三方 mvc 框架，仅仅作为工具使用简单快速完成支付模块的开发，可轻松嵌入到任何系统里。 </p>
 *
 * <p>IJPay 交流群: 723992875</p>
 *
 * <p>Node.js 版: https://gitee.com/javen205/TNWX</p>
 *
 * <p>支付宝支付 Demo</p>
 *
 * @author Javen
 */
@Slf4j
@Component
public class AliPay extends AbstractAliPayApiController {

    @Autowired
    private AliPayBean aliPayBean;

    private AjaxResult result = new AjaxResult();
    /**
     * 普通公钥模式
     */
//     private final static String NOTIFY_URL = "/aliPay/notify_url";
    /**
     * 证书模式
     */
    private final static String NOTIFY_URL = "/alipay/superMarketNotify";
//    private final static String RETURN_URL = "/aliPay/return_url";
    /**
     * 证书模式
     */
    private final static String RETURN_URL = "/aliPay/cert_return_url";

//    private String resourcePath = this.getClass().getClassLoader()
//            .getResource("/").getPath();


    @Override
    public AliPayApiConfig getApiConfig() throws AlipayApiException {
        AliPayApiConfig aliPayApiConfig;
        try {
            aliPayApiConfig = AliPayApiConfigKit.getApiConfig(aliPayBean.getAppId());

        } catch (Exception e) {
            aliPayApiConfig = AliPayApiConfig.builder()
                    .setAppId(aliPayBean.getAppId())
                    .setAliPayPublicKey(aliPayBean.getPublicKey())
                    .setAppCertPath(aliPayBean.getAppCertPath())
                    .setAliPayCertPath(aliPayBean.getAliPayCertPath())
                    .setAliPayRootCertPath(aliPayBean.getAliPayRootCertPath())
                    .setCharset("UTF-8")
                    .setPrivateKey(aliPayBean.getPrivateKey())
                    .setServiceUrl(aliPayBean.getServerUrl())
                    .setSignType("RSA2")
                    // 普通公钥方式
//                    .build();
//                    // 证书模式
                    .buildByCert();
        }
        return aliPayApiConfig;
    }



    /**
     * app支付
     */
    public AjaxResult appPay(WxOrderVO orderVO) {

        try {
            AliPayApiConfigKit.putApiConfig(getApiConfig());
            AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
            model.setBody(orderVO.getBody());
            model.setSubject(orderVO.getBody());
            model.setOutTradeNo(orderVO.getOutTradeNo());
            model.setTimeoutExpress("30m");
            model.setTotalAmount(orderVO.getTotalPrice().setScale(2, RoundingMode.HALF_UP).toString());
            model.setPassbackParams("callback params");
            model.setProductCode("QUICK_MSECURITY_PAY");
            String orderInfo = AliPayApi.appPayToResponse(model, aliPayBean.getDomain() + orderVO.getNotifyUrl()).getBody();
            log.info("orderInfo:{}", orderInfo);
            result.success(orderInfo);
        } catch (AlipayApiException e) {
            e.printStackTrace();
            result.addError("system error:" + e.getMessage());
        }
        return result;
    }

}
